package com.woniu.fly.fairmall.admin.web;

import com.woniu.fly.fairmall.admin.vo.QuestListVo;
import com.woniu.fly.fairmall.core.util.ResponseUtil;
import com.woniu.fly.fairmall.db.entity.QuestAnswer;
import com.woniu.fly.fairmall.db.entity.QuestList;
import com.woniu.fly.fairmall.db.entity.QuestMain;
import com.woniu.fly.fairmall.db.entity.pojo.QuestVo;
import com.woniu.fly.fairmall.db.service.QuestAnswerService;
import com.woniu.fly.fairmall.db.service.QuestListService;
import com.woniu.fly.fairmall.db.service.QuestMainService;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/admin/quest/")
public class AdminQuestController {

    private final Log log = LogFactory.getLog(AdminQuestController.class);

    @Resource
    private QuestMainService questMainService;

    @Resource
    private QuestListService questListService;

    @Resource
    private QuestAnswerService questAnswerService;

    @GetMapping("/list")
    public Object toList() {
        List<Map<String, Object>> maps = questMainService.queryAllQuest();
        return ResponseUtil.ok(maps);
    }

    @GetMapping("/detail")
    public Object detail(Long id) {
        QuestVo questVo = questMainService.queryQuestDetail(id);
        return ResponseUtil.ok(questVo);
    }

    /**
     * 创建问卷
     *
     * @return
     */
    @PostMapping("/create")
    public Object create(@RequestBody com.woniu.fly.fairmall.admin.vo.QuestVo questVo) {
        this.createQuest(questVo);
        return ResponseUtil.ok();
    }

    /**
     * 修改问卷
     *
     * @return
     */
    @PostMapping("/update")
    public Object update(@RequestBody com.woniu.fly.fairmall.admin.vo.QuestVo questVo) {
        this.updateQuest(questVo);
        return ResponseUtil.ok();
    }

    /**
     * 删除问卷
     *
     * @return
     */
    @PostMapping("/delete")
    public Object delete(@RequestBody com.woniu.fly.fairmall.admin.vo.QuestVo questVo) {
        this.deleteQuest(questVo);
        return ResponseUtil.ok();
    }

    @Transactional
    void updateQuest(com.woniu.fly.fairmall.admin.vo.QuestVo questVo) {
        // 问卷主题
        QuestMain questMain = questVo.getQuestMain();
        questMainService.update(questMain);
        // 问卷内容
        List<QuestListVo> questLists = questVo.getQuestLists();
        for (QuestListVo questListVo : questLists) {
            // 设置的问卷问题
            QuestList questList = questListVo.getQuestList();
            questListService.update(questList);
            // 问卷答案
            List<QuestAnswer> questAnswers = questListVo.getQuestAnswers();
            for (QuestAnswer questAnswer : questAnswers) {
                questAnswerService.update(questAnswer);
            }
        }
    }

    @Transactional
    void createQuest(com.woniu.fly.fairmall.admin.vo.QuestVo questVo) {
        // 问卷主题
        QuestMain questMain = questVo.getQuestMain();
        questMainService.insert(questMain);
        // 问卷内容
        List<QuestListVo> questLists = questVo.getQuestLists();
        for (QuestListVo questListVo : questLists) {
            // 设置的问卷问题
            QuestList questList = questListVo.getQuestList();
            questList.setMainId(questMain.getId());
            questListService.insert(questList);
            // 问卷答案
            List<QuestAnswer> questAnswers = questListVo.getQuestAnswers();
            for (QuestAnswer questAnswer : questAnswers) {
                questAnswer.setListId(questList.getId());
                questAnswerService.insert(questAnswer);
            }
        }
    }

    @Transactional
    void deleteQuest(com.woniu.fly.fairmall.admin.vo.QuestVo questVo) {
        // 问卷主题
        QuestMain questMain = questVo.getQuestMain();
        questMainService.update(questMain);
        // 问卷内容
        List<QuestListVo> questLists = questVo.getQuestLists();
        if (CollectionUtils.isEmpty(questLists)) {
            questMain.setDeleted(true);
            questMainService.update(questMain);
        } else {
            for (QuestListVo questListVo : questLists) {
                // 设置的问卷问题
                QuestList questList = questListVo.getQuestList();
                List<QuestAnswer> questAnswers = questListVo.getQuestAnswers();
                if (CollectionUtils.isEmpty(questLists)) {
                    questList.setDeleted(true);
                    questListService.update(questList);
                } else {
                    // 问卷答案
                    for (QuestAnswer questAnswer : questAnswers) {
                        questAnswer.setDeleted(true);
                        questAnswerService.update(questAnswer);
                    }
                }
            }
        }
    }
}
